//@author A0096759E

package testcases;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import storage.ScheduleEvent;
import storage.Storage;

public class StorageTestSort {
	
	private static final String SUCCESS_ALL_EVENTS_CLEARED = "All events have been cleared from your Schedule!";
	private static final String SUCCESS_ADDED_EVENT = "The event \"%s\" has been added successfully to your Schedule!";
	private static final String SUCCESS_SORTED_BY_ALPHABET = "Your Schedule has successfully been sorted in Alphabetical Order!";
	private static final String SUCCESS_SORTED_BY_TYPE = "Your Schedule has successfully been sorted by Type!";
	private static final String SUCCESS_SORTED_BY_TIME = "Your Schedule has successfully been sorted by Time!";
	
	Storage storageTest = new Storage("JUnit Storage Add Del Test.txt");
	
	@Test
	public void test() {
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storageTest.clearAllEvents());
		StringBuilder correctSequence = new StringBuilder();
		
		ScheduleEvent eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "20121991");
		ScheduleEvent eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "21121991", "1200");
		ScheduleEvent eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		ScheduleEvent eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");
		
		correctSequence.append("1. " + eventToAdd1.toString()+"\n\n");
		correctSequence.append("2. " + eventToAdd2.toString()+"\n\n");
		correctSequence.append("3. " + eventToAdd3.toString()+"\n\n");
		correctSequence.append("4. " + eventToAdd4.toString());
		
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storageTest.addEvent(eventToAdd4));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storageTest.addEvent(eventToAdd2));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storageTest.addEvent(eventToAdd3));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storageTest.addEvent(eventToAdd1));
		
		assertEquals(SUCCESS_SORTED_BY_ALPHABET, storageTest.sortByAlphabet());
		
		assertEquals(correctSequence.toString(), storageTest.toString());
		storageTest.saveEvents();
		
		//Sort By Type
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storageTest.clearAllEvents());
		correctSequence = new StringBuilder();
		
		eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "20121991");
		eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "21121991", "1200");
		eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");

		correctSequence.append("1. " + eventToAdd1.toString()+"\n\n");
		correctSequence.append("2. " + eventToAdd4.toString()+"\n\n");
		correctSequence.append("3. " + eventToAdd2.toString()+"\n\n");
		correctSequence.append("4. " + eventToAdd3.toString());
		
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storageTest.addEvent(eventToAdd2));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storageTest.addEvent(eventToAdd3));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storageTest.addEvent(eventToAdd1));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storageTest.addEvent(eventToAdd4));
		
		assertEquals(SUCCESS_SORTED_BY_TYPE, storageTest.sortByType());
		
		assertEquals(correctSequence.toString(), storageTest.toString());
		storageTest.saveEvents();
		
		//Sort By Time
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storageTest.clearAllEvents());
		correctSequence = new StringBuilder();
		
		eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "99999999");
		eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "22121991", "1600");
		eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");

		correctSequence.append("1. " + eventToAdd3.toString()+"\n\n");
		correctSequence.append("2. " + eventToAdd2.toString()+"\n\n");
		correctSequence.append("3. " + eventToAdd4.toString()+"\n\n");
		correctSequence.append("4. " + eventToAdd1.toString());

		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storageTest.addEvent(eventToAdd4));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storageTest.addEvent(eventToAdd2));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storageTest.addEvent(eventToAdd1));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storageTest.addEvent(eventToAdd3));
		
		assertEquals(SUCCESS_SORTED_BY_TIME, storageTest.sortByTime());
		
		assertEquals(correctSequence.toString(), storageTest.toString());
		storageTest.saveEvents();
	}

}
